/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogOrcamentos.java
 *
 * Created on 12/04/2011, 09:40:47
 */
package camada.apresentacao.dialog.orcamento;

import Relatorios.Cliente.ReportOrcamento;
import camada.bancodedados.daoimplemente.orcamento.OrcamentoDAOImpl;
import eloma.classe.orcamento.Orcamento;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ListSelectionModel;
import tabelamodelcolumnmodel.orcamento.ButtonColumn;
import tabelamodelcolumnmodel.orcamento.OrcamentoColumnModel;
import tabelamodelcolumnmodel.orcamento.OrcamentoTableModel;
import utilitarios.Exceptions;

/**
 *
 * @author icaro.klinger
 */
public class DialogOrcamentos extends javax.swing.JDialog {

    OrcamentoDAOImpl daoOrcamento = new OrcamentoDAOImpl();
    Exceptions exceptions = new Exceptions();

    int indexTabela;

    /**
     * Creates new form DialogOrcamentos
     */
    public DialogOrcamentos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setTitle("Orçamentos");
        this.setLocationRelativeTo(null);
        configurarTabela();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaOrcamento = new javax.swing.JTable();
        btNovo = new javax.swing.JButton();
        btRemover = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btImprimir = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tabelaOrcamento.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabelaOrcamento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaOrcamentoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaOrcamento);

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Add.png"))); // NOI18N
        btNovo.setText("Novo orçamento");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Remove.png"))); // NOI18N
        btRemover.setText("Excluir orçamento");
        btRemover.setEnabled(false);

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Editar.png"))); // NOI18N
        btEditar.setText("Editar orçamento");
        btEditar.setEnabled(false);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        btImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Impressao.png"))); // NOI18N
        btImprimir.setText("Imprimir orçamento");
        btImprimir.setEnabled(false);
        btImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btImprimirActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Voltar.png"))); // NOI18N
        jButton5.setText("Voltar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 623, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btNovo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btRemover)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btImprimir))
                    .addComponent(jButton5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNovo)
                    .addComponent(btRemover)
                    .addComponent(btEditar)
                    .addComponent(btImprimir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        DialogCadOrcamento cadOrcamento = new DialogCadOrcamento(null, true);
        cadOrcamento.setVisible(true);

        configurarTabela();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btEditarActionPerformed

    private void btImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btImprimirActionPerformed
        OrcamentoTableModel model = (OrcamentoTableModel) tabelaOrcamento.getModel();
        Orcamento orc = (Orcamento) model.getValoresOrcamento(indexTabela);
        List<Orcamento> listOrc = new ArrayList<Orcamento>();
        listOrc.add(orc);
        //  ReportOrcamento report = new ReportOrcamento(listOrc, "teste");
        //  report.exibirRelatorio();
    }//GEN-LAST:event_btImprimirActionPerformed

    private void tabelaOrcamentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaOrcamentoMouseClicked

        indexTabela = tabelaOrcamento.getSelectedRow();

        btNovo.setEnabled(false);
        btEditar.setEnabled(true);
        btRemover.setEnabled(true);
        btImprimir.setEnabled(true);
    }//GEN-LAST:event_tabelaOrcamentoMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DialogOrcamentos dialog = new DialogOrcamentos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btImprimir;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton jButton5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaOrcamento;
    // End of variables declaration//GEN-END:variables

    private void configurarTabela() {
        try {

            this.setListaOrcamento(daoOrcamento.pesquisarTodosOrcamentos());

            tabelaOrcamento.setAutoCreateColumnsFromModel(false);
            java.awt.FontMetrics fm = tabelaOrcamento.getFontMetrics(tabelaOrcamento.getFont());

            tabelaOrcamento.setColumnModel(new OrcamentoColumnModel(fm));
            tabelaOrcamento.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            ButtonColumn bt = new ButtonColumn(tabelaOrcamento, 3);

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogOrcamentos-configurarTabela");
        }
    }

    public void setListaOrcamento(List<Orcamento> listOrcamento) {
        this.tabelaOrcamento.setModel(new OrcamentoTableModel(listOrcamento));
    }

}
